package com.Servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.User.AdminOperation;
import com.Washer.DormitoryOperation;
import com.Washer.WasherOperation;


/**
 * Servlet implementation class ServletInquire
 */
@WebServlet("/Dormitory")
public class DormitoryInt extends HttpServlet{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DormitoryInt() {
		super();
		// TODO Auto-generated constructor stub

	}
	
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		long startTime = System.currentTimeMillis(); //程序开始记录时间
		
		request.setCharacterEncoding("utf-8"); 
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/json"); 
		
		DormitoryOperation dormitoryOperation=new DormitoryOperation();
	
		String method = request.getParameter("method");
		System.out.println("method:" + method);
		// 获取string
		InputStreamReader reader=new InputStreamReader((ServletInputStream)request.getInputStream(),"utf-8");
        BufferedReader br = new BufferedReader(reader);
        StringBuffer sb = new StringBuffer("");
        String temp;
        while((temp = br.readLine()) != null){
            sb.append(temp);
        }
        br.close();
        String requestString=sb.toString();
        String reString="[]";
		switch (method) {
		case "getDormitory":
	        reString=DormitoryOperation.getDormitory(requestString);
			break;
		case "insertDormitory":
			reString=DormitoryOperation.insertDormitory(requestString);
		default:
			break;
		}
		
        // 输出
		PrintWriter out = response.getWriter();
		out.write(reString.replace("\"", "'"));
		out.close();
		
		long endTime   = System.currentTimeMillis(); //程序结束记录时间
		System.out.println(endTime - startTime); 
	}
		
}
